﻿namespace Rsdn.Janus.VirtualForums.AnswersMy
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using BLToolkit.Data;
    using DataModel;

    internal class MyAnswersMessage : Msg
    {
        public MyAnswersMessage(IServiceProvider provider) 
            : base(provider)
        {
        }

        /// <summary>
        /// Fills the children.
        /// </summary>
        protected override void FillChildren()
        {
            // do nothing    
        }

        /// <summary>
        /// Gets my messages as children.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <returns></returns>
        public static Msg GetMyMessagesAsChildren(IServiceProvider provider)
        {
            MyAnswersMessage msg = new MyAnswersMessage(provider);
            msg.Children = msg.GetMyMessagesList(provider);
            return msg;
        }

        /// <summary>
        /// Gets the messages.
        /// </summary>
        /// <param name="db">The db.</param>
        /// <returns></returns>
        protected IQueryable<IForumMessage> GetMessages(IJanusDataContext db)
        {
            return db.Messages(m => m.UserID == Config.Instance.SelfId); 
        }

        /// <summary>
        /// Fills the counters.
        /// </summary>
        public void FillCounters()
        {
            using (IJanusDataContext db = ServiceProvider.CreateDBContext())
            {
                if (!db.IsValid())
                {
                    return;
                }

                IQueryable<IForumMessage> forumMessages = GetMessages(db);
                RepliesCount = forumMessages.Count();
                UnreadCount = forumMessages.Count(im => !im.IsRead);
                #region SQL
                #endregion
            }
        }


        /// <summary>
        /// Gets my messages list.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <returns></returns>
        public List<MsgBase> GetMyMessagesList(IServiceProvider provider)
        {
            List<MsgBase> ret = null;
            using (IJanusDataContext db = provider.CreateDBContext())
            {
                IQueryable<IForumMessage> messagesOrig = GetMessages(db);

                // order - last DateFirst
                IOrderedQueryable<IForumMessage> messages = messagesOrig.OrderByDescending(m => m.Date);
                ret = LoadMessages(provider, messages, null);

                foreach (MsgBase msg in ret)
                {
                    msg.EndMapping(null);
                }
            }

            return ret;
        }

        /// <summary>
        /// Gets or sets the unread count.
        /// </summary>
        /// <value>The unread count.</value>
        public int UnreadCount
        {
            get
            {
                return RepliesUnread;
            }
            set
            {
                RepliesUnread = value;
            }
        }

    }
}